package graph_interface;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class TeaInsert extends Application
{
	public static String name="曾立";
	public static void setName(String str)
	{
		name=str;
	}
	public static La[] lab =new La[10];
	static int lastchoose=-1;
	@Override
	public void start(Stage stage)
	{	
		lab[0]=new La("用户id");
		lab[1]=new La("姓名");
		lab[2]=new La("年龄");
		lab[3]=new La("性别");
		lab[4]=new La("身高");
		lab[5]=new La("体重");
		lab[6]=new La("代表队");
		lab[7]=new La("领队");
		lab[8]=new La("总积分");
		lab[9]=new La("总名次");
//		lab[10]=new La("");
		getAthlete();
		
		
		Button bt1=new Button("查询信息");
		Button bt2=new Button("添加");
		Button bt3=new Button("执行");
		bt1.setPrefSize(80,20);
		bt2.setPrefSize(80,20);
		bt3.setPrefSize(40,20);
		FlowPane top=new FlowPane(12,10);
		top.setPrefSize(600,100);
		top.setStyle("-fx-border-color:red");
		
		for(int i=0;i<10;i++)
		{
			top.getChildren().add(lab[i].hb);
		}
		top.getChildren().addAll(bt1,bt2,bt3);
		VBox center=new VBox(0);
		
		Record title=new Record("用户编号","项目名称","用户性别","比赛成绩","比赛分数");
		center.getChildren().add(title.hb);
		
		bt1.setOnAction(event->
		{
			if(center.getChildren().size()>=3)
			{
				return;
			}
			Record[] rc=Query(lab[0].lab2.getText());
			for(int i=0;i<rc.length;i++)
			{
				center.getChildren().add(rc[i].hb);
				int k=i;
				rc[i].hb.setOnMouseClicked(e->
				{
					if(lastchoose==-1)
					{
						lastchoose=k;
						rc[k].hb.setStyle("-fx-background-color:gray;");
					}
					else
					{
						rc[lastchoose].hb.setStyle("-fx-background-color:white");//消除原来的选中
						lastchoose=k;
						rc[lastchoose].hb.setStyle("-fx-background-color:gray");
					}
				});
			}
		});
		
		
		bt2.setOnAction(e->
		{
			center.getChildren().retainAll(title.hb);
			NewRecord nrc=new NewRecord();
			center.getChildren().add(nrc.hb);
			Connection conn=DBConnection.getConnection();
			bt3.setOnAction(event->{
				try
				{
					
					PreparedStatement ps=conn.prepareStatement("insert into record values(?,?,?,?,?);");
					ps.setInt(1,Integer.parseInt(nrc.larr[0].getText()));
					ps.setString(2,nrc.larr[1].getText());
					ps.setString(3,nrc.larr[2].getText());
					ps.setInt(4,Integer.parseInt(nrc.larr[3].getText()));
					ps.setInt(5,Integer.parseInt(nrc.larr[4].getText()));
					ps.executeUpdate();
					System.out.println("插入成功");
				}
				catch(Exception ex) {
					System.out.println("插入失败，请重试！！");
				}
			});
		});
		
		
		
		ScrollPane scro=new ScrollPane(center);
		BorderPane root=new BorderPane();
		root.setPadding(new Insets(10));
		root.setTop(top);
		root.setCenter(scro);
		Scene scene=new Scene(root,600,400);
		stage.setScene(scene);
		stage.setTitle("教师界面：欢迎您，"+name);
		stage.show();
	}
	public static void getAthlete()
	{
		Connection conn=DBConnection.getConnection();
		try
		{
			Statement stmt=conn.createStatement();
			ResultSet rs=stmt.executeQuery("select * from athletes where name="+"'"+name+"'");
			rs.next();
			for(int i=0;i<4;i++)
			{
				lab[i].lab2.setText(rs.getString(i+1));
			}
		}
		catch(SQLException e) {
			
		}
		
	}
	public static Record[] Query(String id)
	{
		ArrayList<Record> al=new ArrayList<>();
		Connection conn=DBConnection.getConnection();
		try {
			Statement stmt=conn.createStatement();
			ResultSet rs=stmt.executeQuery("select Sid,Pname,sex,score,pointer from record order by Pname;");
			while(rs.next())
			{
				String sid=String.valueOf(rs.getInt(1));
				String pname=rs.getString(2);
				String sex=rs.getString(3);
				String score=String.valueOf(rs.getInt(4));
				String pointer=String.valueOf(rs.getInt(5));
				Record rc=new Record(sid,pname,sex,score,pointer);
				al.add(rc);
			}
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return al.toArray(new Record[al.size()]);
	}
	public static void main(String[] args)
	{
		Application.launch(args);
	}
}
class NewRecord
{
	HBox hb=new HBox();
	TextField[] larr=new TextField[5];
	public NewRecord()
	{
		for(int i=0;i<larr.length;i++)
		{
			larr[i]=new TextField();
			larr[i].setPromptText("+");
		}
		hb.setStyle("-fx-background-color:gray;");
		for(int i=0;i<larr.length;i++)
		{
			larr[i].setPrefSize(116, 30);
			larr[i].setStyle("-fx-border-color:black;");
			larr[i].setAlignment(Pos.CENTER);
			hb.getChildren().add(larr[i]);
		}
	}
}